<?php

declare(strict_types=1);
/**
 * Created by PhpStorm.
 * User: yangfan
 * Date: 2019/12/12
 * Time: 11:11 AM
 */

namespace App\Entity;

use App\Meta\GenCouponGroupMeta;
use App\Models\CouponGroup;

class CouponGroupEntity extends ABaseEntity
{
    public function __construct(CouponGroup $model)
    {
        $this->model = $model;
    }

    /**
     * 通过模板优惠券组
     *
     * @return CouponGroupEntity
     */
    public static function updateCouponGroupEntityByMeta(self $entity, GenCouponGroupMeta $meta)
    {
        $entity = self::syncMeta2CouponGroupEntity($entity, $meta);
        $entity->save();

        return $entity;
    }

    /**
     * 通过模板生成优惠券组
     *
     * @return CouponGroupEntity
     */
    public static function createCouponGroupEntityByMeta(GenCouponGroupMeta $meta)
    {
        $entity = self::getNewInstance();
        $entity = self::syncMeta2CouponGroupEntity($entity, $meta);
        $entity->save();

        return $entity;
    }

    /**
     * @return int
     */
    public function getId()
    {
        return $this->model->id;
    }

    public function getTitle(): string
    {
        return $this->model->title;
    }

    public function setTitle(string $title): void
    {
        $this->model->title = $title;
    }

    public function getRemark(): string
    {
        return $this->model->remark;
    }

    public function setRemark(string $remark): void
    {
        $this->model->remark = $remark;
    }

    public function getType(): string
    {
        return $this->model->type;
    }

    public function setType(string $type): void
    {
        $this->model->type = $type;
    }

    public function getCount(): int
    {
        return $this->model->count;
    }

    public function setCount(int $count): void
    {
        $this->model->count = $count;
    }

    public function getValue(): string
    {
        return $this->model->value;
    }

    public function setValue(string $value): void
    {
        $this->model->value = $value;
    }

    public function getNotBefore(): string
    {
        return $this->model->not_before ?: '';
    }

    public function setNotBefore(string $notBefore): void
    {
        $this->model->not_before = $notBefore;
    }

    public function getNotAfter(): string
    {
        return $this->model->not_after ?: '';
    }

    public function setNotAfter(string $notAfter): void
    {
        $this->model->not_after = $notAfter;
    }

    /**
     * @return string
     */
    public function getCityIds()
    {
        return $this->model->city_ids ?: '0';  // 0 表示全国可用
    }

    /**
     * @param string $cityIds
     */
    public function setCityIds($cityIds): void
    {
        $this->model->city_ids = $cityIds;
    }

    public function getMinAmount(): int
    {
        return $this->model->min_amount;
    }

    public function setMinAmount(int $minAmount): void
    {
        $this->model->min_amount = $minAmount;
    }

    public function getBookNotBefore(): string
    {
        return $this->model->book_not_before ?: '';
    }

    public function setBookNotBefore(string $bookNotBefore): void
    {
        $this->model->book_not_before = $bookNotBefore;
    }

    public function getBookNotAfter(): string
    {
        return $this->model->book_not_after ?: '';
    }

    public function setBookNotAfter(string $bookNotAfter): void
    {
        $this->model->book_not_after = $bookNotAfter;
    }

    public function getProductIds(): string
    {
        return $this->model->product_ids ?: '';
    }

    public function setProductIds(string $productIdds): void
    {
        $this->model->product_ids = $productIdds;
    }

    public function getBtype(): string
    {
        return $this->model->btypes ?: '';
    }

    public function setBtype(string $btype): void
    {
        $this->model->btypes = $btype;
    }

    public function setReceived(int $received)
    {
        $this->model->received = $received;
    }

    /**
     * @return CouponGroupEntity
     */
    private static function syncMeta2CouponGroupEntity(self $entity, GenCouponGroupMeta $meta)
    {
        $entity->setType($meta->getType());
        $entity->setTitle($meta->getTitle());
        $entity->setValue($meta->getValue());
        $entity->setCount($meta->getCount());
        $entity->setRemark($meta->getRemark());
        $entity->setMinAmount($meta->getMinAmount());

        $entity->setCityIds($meta->getCityIds());
        $entity->setProductIds($meta->getProductIds());

        $entity->setNotBefore($meta->getNotBefore());
        $meta->getNotAfter() && $entity->setNotAfter($meta->getNotAfter());
        $entity->setBookNotBefore($meta->getBookNotBefore());
        $meta->getBookNotAfter() && $entity->setBookNotAfter($meta->getBookNotAfter());

        return $entity;
    }
}
